package com.blockchain.front.servlets.api.user;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.trading.TradingManage;
import com.blockchain.front.servlets.api.AbstractTickerServlet;
import com.blockchain.front.servlets.api.ErrorCode;
import com.blockchain.front.servlets.api.ticker.ResultMessage;
import com.blockchain.front.util.JsonUtil;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.BigDecimalParser;

/**
 * 机器人API接口</br>
 * 下单交易</br>
 * 修改时间：2017年7月24日16:01:35</br>
 */
@WebServlet(value = { "/api/user/trade" })
public class Trade extends AbstractTickerServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		ResultMessage msg = new ResultMessage();
		String market = "";
		String apikey = "";
		int type = 0;
		BigDecimal price = new BigDecimal(0);
		BigDecimal num = new BigDecimal(0);
		String tpass = "";
		try {
			market = request.getParameter("market");
			apikey = request.getParameter("apikey");
			type = Integer.parseInt(request.getParameter("type"));
			price = BigDecimalParser.parse(request.getParameter("price"));
			num = BigDecimalParser.parse(request.getParameter("num"));
			tpass = request.getParameter("tpass");
		} catch (Exception e) {
			msg.error = ErrorCode.CSCU.getErrorCode();
		}
//		System.out.println("下单交易-请求参数" + "市场:" + market + ",公钥:" + apikey + ",类型:" + type + ",价格:" + price + ",数量:"
//				+ num + ",密码:" + tpass);
		try {
			if (!StringHelper.isEmpty(market) && !StringHelper.isEmpty(apikey) && !StringHelper.isEmpty(tpass)
					&& type > 0 && price.compareTo(new BigDecimal(0)) > 0 && num.compareTo(new BigDecimal(0)) > 0) {
				TradingManage tradingManage = serviceSession.getService(TradingManage.class);
				List<Map<String, Object>> list = tradingManage.getMarket(market);
				String coinId = "0";
				if (list.size() > 0) {
					int scid = Integer.parseInt(list.get(0).get("marketId").toString());
					if (type == 1) {
						coinId = tradingManage.tradingMr(scid, price, num, tpass, apikey) + "";
					}
					if (type == 2) {
						coinId = tradingManage.tradingMc(scid, price, num, tpass, apikey) + "";
					}
					msg.data = coinId;
				}
			}
		} catch (Throwable e) {
			System.out.println(e.getMessage());
			msg.error = ErrorCode.CXCU.getErrorCode();
			msg.msg = ErrorCode.CXCU.getName();
		}
		String jsonData = JsonUtil.GsonString(msg);
//		System.out.printf("下单返回结果%s\n", jsonData);
		response.getWriter().write(jsonData);
	}

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}
}
